package com.cf.base.leak.androido;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Build;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

/**
 * @author yangji
 */
@RequiresApi(api = Build.VERSION_CODES.O)
public class FragmentOLeak extends FragmentManager.FragmentLifecycleCallbacks {

    private FragmentOCallback mCallback;

    public FragmentOLeak(FragmentOCallback callback) {
        this.mCallback = callback;
    }

    @Override
    public void onFragmentViewDestroyed(@NonNull FragmentManager fm, @NonNull Fragment f) {
        View view = f.getView();
        if (view != null) {
            mCallback.leakage(view);
        }
    }

    @Override
    public void onFragmentDestroyed(@NonNull FragmentManager fm, @NonNull Fragment f) {
        super.onFragmentDestroyed(fm, f);
        mCallback.leakage(f);
    }

    public void invoke(Activity activity) {
        activity.getFragmentManager().registerFragmentLifecycleCallbacks(this, true);
    }
}
